﻿using System;
using System.IO;
using System.Xml.Schema;
using CommandLineParser.Arguments;
using uTILLIty.ChartBuilder.Commandline.Properties;
using uTILLIty.ChartBuilder.Core.Configuration;
using uTILLIty.ChartBuilder.Core.Extensions;

namespace uTILLIty.ChartBuilder.Commandline
{
	public class ChartBuilderCommand
	{
		[FileArgument('c', FileMustExist = true, Optional = false,
			LongName = "config", Description = "", FullDescription = "")]
		public FileInfo ConfigurationFile;

		[FileArgument('o', FileMustExist = false, Optional = false,
			LongName = "out", Description = "", FullDescription = "")]
		public FileInfo OutputFile;

		public void Execute()
		{
			Core.ChartBuilder builder = GetChartBuilder();

			ChartConfiguration config;
			using (var configData = ConfigurationFile.OpenRead())
			{
				XmlSchemaSet schemas = Core.ChartBuilder.GetSchemas();
				config = ObjectExtensions.Load<ChartConfiguration>(configData, schemas);
			}

			Console.WriteLine(Resources.ProcessingFile, config.Series.Length, ConfigurationFile.Name);
			using (var outStream = OutputFile.Open(FileMode.Create))
				builder.Build(config, outStream);
		}

		protected virtual Core.ChartBuilder GetChartBuilder()
		{
			return new Core.ChartBuilder();
		}

	}
}